<?php

namespace App\Admin\Forms\User;

use App\Wen\Utils\Settings;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UserFinancialForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        global $__MINI_GLOBAL_TENANT_ID__;
        if($__MINI_GLOBAL_TENANT_ID__ > 0){
            return $this->response()
                ->error('无权限.');
        }
        $id = $input['id'];
        $add_blance = round($input['add'] ?? 0, 2);
        if($add_blance == 0){
            return $this->response()
                ->warning('增加或减少为0')
                ->refresh();
        }
        if($add_blance > 0){
            UserUtils::update_user_financial($id, 18, $add_blance, '管理员后台给您增加了'.$add_blance.'余额');
        }else{
            UserUtils::update_user_financial($id, 19, _abs($add_blance), '管理员后台给您减少了'.$add_blance.'余额');
        }
        return $this->response()
            ->success('操作成功.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);
        $this->text('has_coin', '拥有余额')->disable()->default(UserUtils::user_balance($this->payload["id"]));
        $this->text('add', '增加或减少')->help('减少使用负数，最多两位小数')->default(0);
    }
}
